﻿using Data;
using Data.Tables;
using PayrollExpress.Helpers;
using PayrollExpress.Models;
using System;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace PayrollExpress.Controllers
{
    public class AdministrationController : BaseController
    {

        public ActionResult Index()
        {
            try
            {
                return RedirectToAction("Index", "Home");
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        #region Users

        public ActionResult Users(int? workLocationID)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.Users);

                var model = new UserListModel();
                using (var db = new DatabaseContext())
                {
                    model = AdministrationHelper.GetUserListModel(db, User.Identity.Name, workLocationID);
                }

                return View(model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult UserAddEdit(int? workLocationID, int? id)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.Users);

                var model = new UserAddEditModel();

                using (var db = new DatabaseContext())
                {
                    model = AdministrationHelper.GetUserAddEditModel(db, workLocationID, id);
                }

                return View(BaseHelper.PageAddEditSave.UserAddEdit.ToString(), model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult UserSave(UserAddEditModel model, string command)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.Users);

                var db = new DatabaseContext();

                if (command == BaseHelper.PageAction.Add.ToString())
                {
                    if (!WebSecurity.Initialized)
                        WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                    WebSecurity.CreateUserAndAccount(model.Type.UserName, model.Password);

                    db.Users.Add(model.Type);
                    db = AdministrationHelper.AddNewUserAccess(db, model);

                    db.SaveChanges();
                }
                else if (command == BaseHelper.PageAction.Edit.ToString())
                {
                    db.Entry(model.Type).State = System.Data.Entity.EntityState.Modified;
                    db = AdministrationHelper.RemoveOldUserAccess(db, model);
                    db = AdministrationHelper.AddNewUserAccess(db, model);
                    db.SaveChanges();
                }

                return RedirectToAction(BaseHelper.Page.Users.ToString(), new { workLocationID = model.Type.WorkLocationID });

            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult UsersRefresh(UserListModel model)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.Users);

                return RedirectToAction(BaseHelper.Page.Users.ToString(),
                    new { workLocationID = model.SelectedWorkLocationID });
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        #endregion

        #region Employment Types

        public ActionResult EmploymentTypes()
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.EmploymentTypes);

                var model = new EmploymentTypeListModel();
                using (var db = new DatabaseContext())
                {
                    model = AdministrationHelper.GetEmploymentTypeListModel(db);
                }

                return View(model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult EmploymentTypeAddEdit(int? id)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.EmploymentTypes);

                var model = new EmploymentTypeAddEditModel();
                using (var db = new DatabaseContext())
                {
                    model = AdministrationHelper.GetEmploymentTypeAddEditModel(db, id);
                }
                
                return View(BaseHelper.PageAddEditSave.EmploymentTypeAddEdit.ToString(), model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult EmploymentTypeSave(EmploymentTypeAddEditModel model, string command)
        {
            try
            {
                using(var db = new DatabaseContext())
                {
                    if (command == BaseHelper.PageAction.Add.ToString())
                    {
                        db.EmploymentTypes.Add(model.Type);
                        db.SaveChanges();
                    }
                    else if (command == BaseHelper.PageAction.Edit.ToString())
                    {
                        db.Entry(model.Type).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        // cancelled
                    }
                }

                return RedirectToAction(BaseHelper.Page.EmploymentTypes.ToString());
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        #endregion

        #region Recurring Holidays

        public ActionResult RecurringHolidays()
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.RecurringHolidays);

                var model = new RecurringHolidayListModel();
                using (var db = new DatabaseContext())
                {
                    model = AdministrationHelper.GetRecurringHolidayListModel(db);
                }

                return View(model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult RecurringHolidayAddEdit(int? id)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.RecurringHolidays);

                var model = new RecurringHolidayAddEditModel();
                using (var db = new DatabaseContext())
                {
                    model = AdministrationHelper.GetRecurringHolidayAddEditModel(db, id);
                }

                return View(BaseHelper.PageAddEditSave.RecurringHolidayAddEdit.ToString(), model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult RecurringHolidaySave(RecurringHolidayAddEditModel model, string command)
        {
            try
            {
                using (var db = new DatabaseContext())
                {
                    if (command == BaseHelper.PageAction.Add.ToString())
                    {
                        db.RecurringHolidays.Add(model.Type);
                        db.SaveChanges();
                    }
                    else if (command == BaseHelper.PageAction.Edit.ToString())
                    {
                        db.Entry(model.Type).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        // cancelled
                    }
                }

                return RedirectToAction(BaseHelper.Page.RecurringHolidays.ToString());
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult GetDays(string month)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.RecurringHolidays);

                var days = AdministrationHelper.CalendarDayDropDownList(int.Parse(month));

                return Json(days, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        #endregion

        #region Work Locations

        public ActionResult WorkLocations()
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.WorkLocations);

                var model = new WorkLocationListModel();
                using (var db = new DatabaseContext())
                {
                    model = AdministrationHelper.GetWorkLocationListModel(db);
                }

                return View(model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult AddWorkLocation()
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.WorkLocations);

                var model = new WorkLocationAddEditModel();

                using (var db = new DatabaseContext())
                {
                    model = AdministrationHelper.GetWorkLocationAddEditModel(db);
                }

                return View(BaseHelper.PageAddEditSave.WorkLocationAddEdit.ToString(), model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult WorkLocationAddEdit(int? id)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.WorkLocations);

                var model = new WorkLocationAddEditModel();
                using (var db = new DatabaseContext())
                {
                    model = AdministrationHelper.GetWorkLocationAddEditModel(db, id);
                }

                return View(BaseHelper.PageAddEditSave.WorkLocationAddEdit.ToString(), model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult WorkLocationSave(WorkLocationAddEditModel model, string command)
        {
            try
            {
                using (var db = new DatabaseContext())
                {
                    if (command == BaseHelper.PageAction.Add.ToString())
                    {
                        db.WorkLocations.Add(model.Type);
                        db.SaveChanges();
                    }
                    else if (command == BaseHelper.PageAction.Edit.ToString())
                    {
                        db.Entry(model.Type).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        // cancelled
                    }
                }

                return RedirectToAction(BaseHelper.Page.WorkLocations.ToString());
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }
        
        #endregion


    }
}
